/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Module.DecisionList;
import Module.Session;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 * @author CCD gruppe 2
 */

public class DecisionListFrame extends javax.swing.JFrame {

    /** Creates new form DecisionList */
    public DecisionListFrame() {
        initComponents();
    }
    private javax.swing.JTable decisionListTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea introTextArea;
    private javax.swing.JButton closeDecisionListButton;
    private javax.swing.JButton editDecisionButton;
    private javax.swing.JButton newDecisionButton;
    @SuppressWarnings("unchecked")
    private javax.swing.JButton saveDecisionListButton;

    private int selectedDecsisionListItem;
    private JComboBox status;
    private JComboBox session;
    ArrayList<String> statusString;
    ArrayList<String> sessionString;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">

    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        introTextArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jLabel3 = new javax.swing.JLabel();
        decisionListTable = new javax.swing.JTable();
        jLabel1.setText("Beslutningsliste");
        editDecisionButton = new javax.swing.JButton();
        newDecisionButton = new javax.swing.JButton();
        closeDecisionListButton = new javax.swing.JButton();
        jLabel2.setText("Introduksjon");
        status = new JComboBox();
        session = new JComboBox();
        status.removeAllItems();
        session.removeAllItems();
        status.addItem("Til beslutning");
        status.addItem("Besluttet");
        status.addItem("Avsluttet");
        saveDecisionListButton = new javax.swing.JButton();


        introTextArea.setColumns(20);
        introTextArea.setRows(5);
        introTextArea.setLineWrap(true);
        jScrollPane1.setViewportView(introTextArea);


        jScrollPane2.setViewportView(decisionListTable);

        jLabel3.setText("Beslutningsliste");
        editDecisionButton.setText("Angre");

        newDecisionButton.setText("Ny beslutning");

        closeDecisionListButton.setText("Lukk");

        saveDecisionListButton.setText("Lagre");


        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(223, 223, 223)
                            .add(jLabel1))
                        .add(layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jLabel2))
                        .add(layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jLabel3))
                        .add(layout.createSequentialGroup()
                            .addContainerGap()
                            .add(editDecisionButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 114, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(18, 18, 18)
                            .add(newDecisionButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 127, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(18, 18, 18)
                            .add(closeDecisionListButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 133, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(14, 14, 14)
                            .add(saveDecisionListButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 127, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 540, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 540, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(18, 18, 18)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 107, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 321, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(13, 13, 13)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(editDecisionButton)
                    .add(newDecisionButton)
                    .add(saveDecisionListButton)
                    .add(closeDecisionListButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
        setCenterPos(this);

    }// </editor-fold>

    private static void setCenterPos(JFrame frame) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((screen.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((screen.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);
    }

    /*
     * Getters and setters
     */
    public JTable getDecisionListTable() {
        return decisionListTable;
    }

    public void setDecisionListTable(JTable DecisionListTable) {
        this.decisionListTable = DecisionListTable;
    }

    public JTextArea getIntroTextArea() {
        return introTextArea;
    }

    public void setIntroTextArea(JTextArea introTextArea) {
        this.introTextArea = introTextArea;
    }

    private void setUpSessionName(JTable table, TableColumn sessionColum, ArrayList<Session> sessions, String projectName) {
        session.removeAllItems();
        session.addItem(projectName);
        for (int i = 0; i < sessions.size(); i++) {
            session.addItem(sessions.get(i).getName());
        }
        sessionColum.setCellEditor(new DefaultCellEditor(session));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        sessionColum.setCellRenderer(renderer);
    }


    public void setTabel(ArrayList<DecisionList> liste, ArrayList<Session> sessions,String projectName) {

        decisionListTable = new javax.swing.JTable(liste.size(), 3);
        setUpSessionName(decisionListTable, decisionListTable.getColumnModel().getColumn(0), sessions,projectName);
        decisionListTable.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(status));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        decisionListTable.getColumnModel().getColumn(2).setCellRenderer(renderer);
        decisionListTable.getColumnModel().getColumn(0).setHeaderValue("Tilknytning");
        decisionListTable.getColumnModel().getColumn(1).setHeaderValue("Beskrivelse");
        decisionListTable.getColumnModel().getColumn(2).setHeaderValue("Status");
        String sessionName="";
        for (int i = 0; i < liste.size(); i++) {
            for (int j = 0; j < sessions.size(); j++) {
                if (sessions.get(j).getId() == liste.get(i).getSessionId()) {
                    sessionName=sessions.get(j).getName();
                    j = sessions.size() + 1;
                }
            }
            
            if(liste.get(i).getSessionId()==0)decisionListTable.getModel().setValueAt(projectName, i, 0);
            else decisionListTable.getModel().setValueAt(sessionName, i, 0);
            decisionListTable.getModel().setValueAt(liste.get(i).getDescription(), i, 1);
            decisionListTable.getModel().setValueAt(liste.get(i).getStatus(), i, 2);

            decisionListTable.getColumnModel().getColumn(1).setCellRenderer(new TextAreaRenderer());
            if (liste.get(i).getDescription().length() > 30) {
                int lines = (int) liste.get(i).getDescription().length() / 25;
                lines+=1;
                decisionListTable.setRowHeight(i, decisionListTable.getRowHeight() * lines);
            }

        }

        jScrollPane2.setViewportView(decisionListTable);
    }

    //Privates classes to be used to formate text
    private class TextAreaRenderer extends JTextArea
            implements TableCellRenderer {

        public TextAreaRenderer() {
            setLineWrap(true);
            setWrapStyleWord(true);
        }

        public Component getTableCellRendererComponent(JTable jTable,
                Object obj, boolean isSelected, boolean hasFocus, int row,
                int column) {
            setText((String) obj);
            return this;
        }
    }

    

    public JButton getCloseDecisionListButton() {
        return closeDecisionListButton;
    }

    public void setCloseDecisionListButton(JButton closeDecisionListButton) {
        this.closeDecisionListButton = closeDecisionListButton;
    }

    public JButton getEditDecisionButton() {
        return editDecisionButton;
    }

    public void setEditDecisionButton(JButton editDecisionButton) {
        this.editDecisionButton = editDecisionButton;
    }

    public JButton getNewDecisionButton() {
        return newDecisionButton;
    }

    public void setNewDecisionButton(JButton newDecisionButton) {
        this.newDecisionButton = newDecisionButton;
    }

    public int getSelectedDecsisionListItem() {
        return selectedDecsisionListItem;
    }

    public void setSelectedDecsisionListItem(int selectedDecsisionListItem) {
        this.selectedDecsisionListItem = selectedDecsisionListItem;
    }

    public JButton getSaveDecisionListButton() {
        return saveDecisionListButton;
    }

    public void setSaveDecisionListButton(JButton saveDecisionListButton) {
        this.saveDecisionListButton = saveDecisionListButton;
    }

}
